package com.kinghunt.misp.web.controller.devicex;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceInspectionDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.devicex.DeviceInspectionParam;
import com.kinghunt.misp.service.api.devicex.DeviceInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 设备巡检管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/deviceInspection/")
public class DeviceInspectionController {

    @Autowired(required = false)
    private DeviceInspectionService deviceInspectionService;

    /**
     * 列表显示：显示设备巡检列表
     */
    @GetMapping("getDeviceInspectionList")
    public ApiResult<PageInfo<DeviceInspectionDTO>> getDeviceInspectionList(Long deviceId, int pageNum, int pageSize) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入耗材信息！");
        }
        DeviceInspectionParam deviceInspectionParam = new DeviceInspectionParam();
        deviceInspectionParam.setDeviceId(deviceId);
        deviceInspectionParam.setPageNum(pageNum);
        deviceInspectionParam.setPageSize(pageSize);
        //UserDTO loginUser = getCurrentUser();
        return deviceInspectionService.getDeviceInspectionList(deviceInspectionParam);
    }
    /**
     * 设备详情展示
     */
    @GetMapping("getDeviceInspectionDetail")
    public ApiResult<DeviceInspectionDTO> getDeviceInspectionDetail(Long deviceInspectionId) {
        if(Objects.isNull(deviceInspectionId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        return deviceInspectionService.getDeviceInspectionDetail(deviceInspectionId);
    }
    /**
     * 新增设备巡检
     */
    @PostMapping(value = "addDeviceInspection", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addDeviceInspection(@Valid @RequestBody DeviceInspectionParam deviceInspectionParam) {
        if(Objects.isNull(deviceInspectionParam.getDeviceId())){
            return ApiResult.failOfMessage("新增失败，请输入设备巡检信息！");
        }
        UserDTO loginUser = getCurrentUser();
        deviceInspectionParam.setCreatorId(loginUser.getId());
        return deviceInspectionService.addDeviceInspection(deviceInspectionParam);
    }
}
